use "$temp\cleaned_obs_ver$ver.dta", clear
drop if missing(spell)

merge 1:1 persnr spell using "$temp\BeH_large", keep(3) nogen

merge m:1 betnr using "$temp/estab_matrix_ids_ver$ver.dta", keep(3) nogen

keep if in_strconn == 1

dis as text "syear = $syear ; eyear = $eyear"
drop if year < $syear 
drop if year > $eyear

gen ln_imp_w = log(tentgelt)
gen ln_imp_earn = ln_imp_w + log(spell_length)

tab beh_ausbildung
gen edu = beh_ausbildung
replace edu = 0 if beh_ausbildung == -7
replace edu = 5 if edu == 6
tab edu

compress
keep ln_imp_w age edu year persnr betnr ao_bula


reghdfe ln_imp_w, absorb(i.age#i.edu year persnr betFE=betnr)

if inlist($ver , 3 , 31 ,32) { 
	egen double betnryear = group(betnr year)
	reghdfe ln_imp_w, absorb(i.age#i.edu persnr betFEtv=betnryear) 
	keep betnr persnr year *FE*
	compress
	save "$temp\tempAKM", replace
}


keep betnr persnr year *FE*
compress
	
collapse (mean) betFE, by(betnr) fast
rename betFE firm_fe
save "$temp\AKM_wages_ver$ver", replace


if inlist($ver , 3 , 31 ,32) {
	use "$temp\tempAKM", clear
	collapse (mean) betFEtv, by(betnr year) fast
	rename betFEtv firm_fetv
	save "$temp\AKM_wages_tv_ver$ver", replace
}

clear
